﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebIG.Data;
using WebIG.Service;

namespace WebIG.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IEndpoint _endpoint = new IGEndpoint();

        public ActionResult Index(string code)
        {
            var atoken = Request.Cookies["atoken"];
            var userid = Request.Cookies["userid"];

            User user = null;

            if (atoken == null || userid == null)
            {
                if (code != null)
                {
                    //Request the access_token
                    var authResult = _endpoint.RequestAccessToken(code);
                    atoken = new HttpCookie("atoken", authResult.AccessToken);
                    atoken.Expires.AddDays(365);

                    userid = new HttpCookie("userid", authResult.User.Id);
                    userid.Expires.AddDays(365);

                    HttpContext.Response.Cookies.Add(atoken);
                    HttpContext.Response.Cookies.Add(userid);

                    user = authResult.User;
                }
                else
                {
                    return View(new List<Feed>());
                }
            }

            var feeds = _endpoint.GetSelfFeeds(atoken.Value);
            if (user == null)
            {
                user = _endpoint.GetUser(userid.Value, atoken.Value);
            }

            Session["user"] = user;

            return View(feeds);
        }

        [HttpGet]
        public JsonResult LoadMore(int type, string maxId, string userId)
        {
            //type 0: self feed, 1: user recent
            var accessToken = Session["AccessToken"].ToString();
            var feeds = _endpoint.LoadMoreFeeds(type, userId, maxId, accessToken);

            return Json(feeds, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Media(string id)
        {
            var accessToken = Session["AccessToken"].ToString();

            var feed = _endpoint.GetMedia(id, accessToken);

            return View(feed);
        }

        [HttpPost]
        public JsonResult SetMediaLike(string mediaId)
        {
            var accessToken = Session["AccessToken"].ToString();

            _endpoint.SetMediaLike(mediaId, accessToken);

            return Json(true);
        }

        [HttpPost]
        public JsonResult DelMediaLike(string mediaId)
        {
            var accessToken = Session["AccessToken"].ToString();

            _endpoint.DelMediaLike(mediaId, accessToken);

            return Json(true);
        }

        public ActionResult User(string id)
        {
            var accessToken = Session["AccessToken"].ToString();

            var feed = _endpoint.GetUserMedia(id, accessToken);

            ViewBag.User = _endpoint.GetUser(id, accessToken);

            var relationship = _endpoint.GetRelationship(id, accessToken).Data;

            string relation = "";
            if (relationship.OutgoingStatus == "follows")
            {
                relation = "Following";
            }
            else if (relationship.OutgoingStatus == "requested")
            {
                relation = "Requested";
            }
            else
            {
                relation = "Follow";
            }

            ViewBag.Relation = relation;

            return View(feed);
        }

        [HttpPost]
        public JsonResult ModifyRelationship(string userId, string status)
        {
            var accessToken = Session["AccessToken"].ToString();

            string action = "";
            if (status == "Follow")
            {
                action = "follow";
            }
            else if (status == "Following")
            {
                action = "unfollow";
            }

            var response = _endpoint.ModifyRelationship(userId, accessToken, action);

            if (response.Meta.Code == 200)
                return Json(response.Data.OutgoingStatus);
            else
                return Json(false);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}